import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Column, Entity, JoinTable, ManyToMany, OneToMany, Relation } from 'typeorm';

import { CommonEntity, CommonStatus } from '~/common/entity/common.entity';

import { UserEntity } from '../../user/user.entity';
import { MenuEntity } from '../menu/menu.entity';

export enum RoleValueEnum {
  SUPER_ADMIN = 'SUPER_ADMIN',
  ADMIN = 'ADMIN',
  USER = 'USER',
}

@Entity({ name: 'sys_role' })
export class RoleEntity extends CommonEntity {
  @Column({ type: 'varchar', length: 50, unique: true })
  name: string;

  @Column({ type: 'enum', enum: RoleValueEnum, unique: true, comment: '角色值' })
  value: string;

  @Column({ type: 'text', nullable: true, comment: '角色描述' })
  remark: string;

  @Column({ type: 'tinyint', width: 1, default: CommonStatus.YES, comment: '状态' })
  status: number;

  @ApiHideProperty()
  @OneToMany(() => UserEntity, (user) => user.role)
  users: Relation<UserEntity[]>;

  @ApiHideProperty()
  @ManyToMany(() => MenuEntity, (menu) => menu.roles, {})
  @JoinTable({
    name: 'sys_role_menus',
    joinColumn: { name: 'role_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'menu_id', referencedColumnName: 'id' },
  })
  menus: Relation<MenuEntity[]>;
}
